// Tonemapping, Sharpen, and Desaturation effects for SA - DKT70 - Feb 2011.
//NEW: Vignette integrated and the icelaglace filmiccurve by Marty McFly

#include "EffectA.txt"

//Vignette

float2 VignetteCenter = (0.500,0.500); 
float VignetteRadius = 0.00;
float VignetteAmount = 0.0;

//--------------------------------------------------------------------------------------
// Textures
//--------------------------------------------------------------------------------------
texture2D texColor;

//--------------------------------------------------------------------------------------
// Sampler Inputs
//--------------------------------------------------------------------------------------


sampler2D InputSampler = sampler_state
{
     Texture = (texColor);
     MinFilter = Point;
     MagFilter = Anisotropic;
     MipFilter = Point;
     AddressU  = Clamp;
     AddressV  = Clamp;
     SRGBTexture=FALSE;
     MaxMipLevel=0;
     MipMapLodBias=0;
};


struct VS_OUTPUT_POST {
 float4 vpos  : POSITION;
 float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST {
 float3 pos  : POSITION;
 float2 txcoord : TEXCOORD0;
};

float pixelWidth;
float pixelHeight;


//--------------------------------------------------------------------------------------
// Vertex Shader Input
//--------------------------------------------------------------------------------------

VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
 VS_OUTPUT_POST OUT;

 float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

 OUT.vpos=pos;
 OUT.txcoord.xy=IN.txcoord.xy;

 return OUT;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 c = tex2D(InputSampler, uv);
	 
    c.rgb = max(0, c.rgb - Defog * FogColor.rgb);
    c.rgb *= pow(2.0f, Exposure);
    c.rgb = pow(c.rgb, Gamma);
	
    float2 tc = uv - VignetteCenter;
    float v = length(tc) / VignetteRadius;
    c.rgb += pow(v, 4) * VignetteAmount;

    float3 d = c.rgb * float3(1.05f, 0.97f, 1.27f);
    c.rgb = lerp(c.rgb, d, BlueShift);
 
    float4 curr = ((c*(A*c+C*B)+D*E)/(c*(A*c+B)+D*F))-E/F;
    float4 whiteScale = ((W*(A*W+C*B)+D*E)/(W*(A*W+B)+D*F))-E/F;
    c = curr*whiteScale;

    float k = 0.1;
    float kcube = 0.1;
    float r2 = (uv.x-0.5) * (uv.x-0.5) + (uv.y-0.5) * (uv.y-0.5);       
    float f = 0;
            
//f = 1 + r2 * k;
      
f = 1 + r2 * (k + kcube * sqrt(r2)); 
 
// Lens distortion values
float ChromaticAmount = 0.000; // Amount of chromatic aberration on the edges
float LensSize = 0.55;   // 0.5 = Original image size; 1.0 = Zoomed

float3 eta = float3(1.0+ChromaticAmount*0.0,1.0+ChromaticAmount*0.0,1.0+ChromaticAmount*0.0);
      
float LensZoom = 1.0/LensSize;

        float x = f*LensZoom*(uv.x-0.5)+0.5;
        float y = f*LensZoom*(uv.y-0.5)+0.5;
  
float2 center;
center.x = uv.x-0.5;
center.y = uv.y-0.5;

float2 rCoords = (f*eta.r)*LensZoom*(center.xy*0.5)+0.5;
float2 gCoords = (f*eta.g)*LensZoom*(center.xy*0.5)+0.5;
float2 bCoords = (f*eta.b)*LensZoom*(center.xy*0.5)+0.5;    

float4 inputDistord = float4(tex2D(InputSampler,rCoords).r , tex2D(InputSampler,gCoords).g ,tex2D(InputSampler,bCoords).b, tex2D(InputSampler,float2(x,y)).a);

return c, float4(inputDistord.r,inputDistord.g,inputDistord.b,1);


  //float3 inputDistord = tex2D(InputSampler,float2(x,y));

//return float4(inputDistord.r,inputDistord.g,inputDistord.b,1); 
  
float2 InputSize = float2(sxres, syres/aspect);
    float Amount = sharps;
    float2 offset = offsetv / InputSize;
    float4 color;
    color = tex2D(InputSampler, uv);
    color += tex2D(InputSampler, uv - offset) * Amount;
    color -= tex2D(InputSampler, uv + offset) * Amount;
	
	float middlegray=(c.r+c.g+c.b)*0.333;
	float3 diffcolor=c.rgb-middlegray;
	c.rgb+=diffcolor*-sat;

    return c * color; 
}




//--------------------------------------------------------------------------------------
// Compiler
//--------------------------------------------------------------------------------------
technique PostProcess
{
    pass P0
    {
#ifdef E_SHADER_3_0
 VertexShader = compile vs_3_0 VS_PostProcess();
 PixelShader  = compile ps_3_0 main();
#else
 VertexShader = compile vs_2_0 VS_PostProcess();
 PixelShader  = compile ps_2_0 main();
#endif

 ZEnable=FALSE;
 CullMode=NONE;
 ALPHATESTENABLE=FALSE;
 SEPARATEALPHABLENDENABLE=FALSE;
 AlphaBlendEnable=FALSE;
 FogEnable=FALSE;
 SRGBWRITEENABLE=FALSE;
 }
}
